// checkPayment.js
function queryProduct(productId) {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.open('GET', `http://localhost:3999/query-product/${productId}`, true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    const response = JSON.parse(xhr.responseText);
                    resolve(response);
                } else {
                    reject(new Error('Failed to query product'));
                }
            }
        };
        xhr.send();
    });
}
function checkPayment(orderNo) {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.open('GET', `http://localhost:3999/check-order/${orderNo}`, true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    const response = JSON.parse(xhr.responseText);
                    resolve(response);
                } else {
                    reject(new Error('Failed to check payment status'));
                }
            }
        };
        xhr.send();
    });
}

function displayProductInfo(productId) {
    queryProduct(productId).then(response => {
        const productInfoContainer = document.getElementById('product-info');
        if (response.status === 'success') {
            productInfoContainer.innerHTML = `商品名:${response.data.ProductName}，价格:${response.data.Price}`;
        } else {
            productInfoContainer.innerHTML = '商品信息异常';
        }
    });
}

function displayPaymentStatus(orderNo) {
    checkPayment(orderNo).then(response => {
        const passwordContainer = document.getElementById('password-container');
        if (response.status === 'success') {
            passwordContainer.innerHTML = `支付成功，密码信息为: ${response.data.secrets}`;
        } else {
            passwordContainer.innerHTML = '支付状态: 待支付';
        }
    }).catch(error => {
        console.error('Error:', error);
    });
}

function generateOrderNo(productId) {
    const timestamp = Date.now();
    const randomNum = Math.floor(Math.random() * 1000) + 1;
    return `${productId}-${timestamp}-${randomNum}`;
    // const orderNoContainer = document.getElementById('order-no');
    // orderNoContainer.innerHTML = `${productId}-${timestamp}-${randomNum}`;
}
